using System.Collections;
using System.Collections.Generic;
using MY_FSM;
using UnityEngine;
using System;

[Serializable]
public class ZombieBlackBoard : Blackboard
{
    public float idleTime;//空闲时间
    public float moveSpeed;//移动速度
    public Transform transform;//自己位置
    public Vector3 targetPosition;//随机出来的目标位置
}

public class AI_IdleState : IState
{
    private float idleTime;
    private FSM fsm;
    private ZombieBlackBoard blackboard;
    public AI_IdleState(FSM fsm)
    {
        this.fsm = fsm;
        blackboard = fsm.blackboard as ZombieBlackBoard;
    }
    
    public void OnEnter()
    {
        idleTime = 0;
    }

    public void OnExit()
    {
        
    }

    public void OnUpdate()
    {
        idleTime += Time.deltaTime;
        if (idleTime > blackboard.idleTime)
        {
            fsm.SwitchState(MY_FSM.StateType.Move);
        }
    }
}

public class AI_MoveState : IState
{
    private float idleTime;
    private FSM fsm;
    private ZombieBlackBoard blackboard;
    // private Vector3 targetPos;
    public AI_MoveState(FSM fsm)
    {
        this.fsm = fsm;
        blackboard = fsm.blackboard as ZombieBlackBoard;
    }
    public void OnEnter()
    {
        float x = UnityEngine.Random.Range(-20, 20);
        float z = UnityEngine.Random.Range(-20, 20);
        //当前位置从黑板中获取
        blackboard.targetPosition = new Vector3(blackboard.transform.position.x + x, 2, blackboard.transform.position.z + z);
    }

    public void OnExit()
    {
        
    }

    public void OnUpdate()
    {
        //判断当前距离目标位置是否小于某个值
        if (Vector3.Distance(blackboard.transform.position, blackboard.targetPosition) < 0.1f)
        {
            fsm.SwitchState(MY_FSM.StateType.Idle);//切换到空闲状态
        }
        else
        {
            //移动到目标位置
            blackboard.transform.position = Vector3.MoveTowards(blackboard.transform.position, blackboard.targetPosition, blackboard.moveSpeed * Time.deltaTime);
        }
        
    }
    
}


public class AiNew : MonoBehaviour
{
    // Start is called before the first frame update
    private FSM fsm;
    public ZombieBlackBoard blackboard;
    void Start()
    {
        fsm = new FSM(blackboard);
        fsm.AddState(MY_FSM.StateType.Idle, new AI_IdleState(fsm));
        fsm.AddState(MY_FSM.StateType.Move, new AI_MoveState(fsm));
        fsm.SwitchState(MY_FSM.StateType.Idle);
    }

    // Update is called once per frame
    void Update()
    {
        fsm.OnCheck();
        fsm.OnUpdate();
        // Flip(); //旋转朝向
    }

    void Flip()
    {
        if (blackboard.targetPosition != Vector3.zero)
        {
            transform.localScale = new Vector3(transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
        }
        else
        {
            transform.localScale = new Vector3(transform.localScale.x * 1, transform.localScale.y, transform.localScale.z);
        }
    }

    private void FixedUpdate()
    {
        fsm.OnCheck();
        fsm.OnFixedUpdate();
    }
}
